<template>
  <el-dialog
    :title="!dataForm.metroId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地铁名称" prop="metroName">
      <el-input v-model="dataForm.metroName" placeholder="地铁名称"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="provinceName">
      <el-input v-model="dataForm.provinceName" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="cityName">
      <el-input v-model="dataForm.cityName" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="metroSort">
      <el-input v-model="dataForm.metroSort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="启用状态(1.启用 2.禁用)" prop="metroStatus">
      <el-input v-model="dataForm.metroStatus" placeholder="启用状态(1.启用 2.禁用)"></el-input>
    </el-form-item>
    <el-form-item label="坐标位置" prop="metroCoordinates">
      <el-input v-model="dataForm.metroCoordinates" placeholder="坐标位置"></el-input>
    </el-form-item>
    <el-form-item label="站点地址" prop="metroAddress">
      <el-input v-model="dataForm.metroAddress" placeholder="站点地址"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          metroId: 0,
          metroName: '',
          provinceName: '',
          cityName: '',
          metroSort: '',
          metroStatus: '',
          metroCoordinates: '',
          metroAddress: ''
        },
        dataRule: {
          metroName: [
            { required: true, message: '地铁名称不能为空', trigger: 'blur' }
          ],
          provinceName: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          cityName: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          metroSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          metroStatus: [
            { required: true, message: '启用状态(1.启用 2.禁用)不能为空', trigger: 'blur' }
          ],
          metroCoordinates: [
            { required: true, message: '坐标位置不能为空', trigger: 'blur' }
          ],
          metroAddress: [
            { required: true, message: '站点地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.metroId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.metroId) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/metro/info/${this.dataForm.metroId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.metroName = data.metro.metroName
                this.dataForm.provinceName = data.metro.provinceName
                this.dataForm.cityName = data.metro.cityName
                this.dataForm.metroSort = data.metro.metroSort
                this.dataForm.metroStatus = data.metro.metroStatus
                this.dataForm.metroCoordinates = data.metro.metroCoordinates
                this.dataForm.metroAddress = data.metro.metroAddress
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/metro/${!this.dataForm.metroId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'metroId': this.dataForm.metroId || undefined,
                'metroName': this.dataForm.metroName,
                'provinceName': this.dataForm.provinceName,
                'cityName': this.dataForm.cityName,
                'metroSort': this.dataForm.metroSort,
                'metroStatus': this.dataForm.metroStatus,
                'metroCoordinates': this.dataForm.metroCoordinates,
                'metroAddress': this.dataForm.metroAddress
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
